package com.newtouch.xcd.common.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.IOException;

/**
 * Scp端口工具，利用22端口，实现内网的文件传输
 */
public class ScpUtils {

    //    private static String IP = "120.132.2.234";
    private static String IP = "192.168.254.100";
    private static int PORT = 22;
    //    private static String USER = "root";//登录用户名
    private static String USER = "nvidia";//登录用户名
//    private static String PASSWORD = "04f3a27219d7a9bb028cd433efde629d";//生成私钥的密码和登录密码，这两个共用这个密码

    private static String PASSWORD = "nvidia";//生成私钥的密码和登录密码，这两个共用这个密码
    private static Connection connection = new Connection(IP, PORT);
    private static String PRIVATEKEY = "C:\\Users\\ubuntu\\.ssh\\id_rsa";// 本机的私钥文件
    private static boolean usePassword = true;// 使用用户名和密码来进行登录验证。如果为true则通过用户名和密码登录，false则使用rsa免密码登录

    /**
     * ssh用户登录验证，使用用户名和密码来认证
     *
     * @param user
     * @param password
     * @return
     */
    public static boolean isAuthedWithPassword(String user, String password) {
        try {
            return connection.authenticateWithPassword(user, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * ssh用户登录验证，使用用户名、私钥、密码来认证 其中密码如果没有可以为null，生成私钥的时候如果没有输入密码，则密码参数为null
     *
     * @param user
     * @param privateKey
     * @param password
     * @return
     */
    public static boolean isAuthedWithPublicKey(String user, File privateKey, String password) {
        try {
            return connection.authenticateWithPublicKey(user, privateKey, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 使用用户名密码登陆
     *
     * @param user     用户名
     * @param password 密码
     * @return
     */
    public static boolean isAuth(Connection con, String user, String password) {
        try {
            return con.authenticateWithPassword(user, password);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean isAuth() {
        if (usePassword) {
            return isAuthedWithPassword(USER, PASSWORD);
        } else {
            return isAuthedWithPublicKey(USER, new File(PRIVATEKEY), PASSWORD);
        }
    }

    public static void getFile(String remoteFile, String path, String ip, Integer port, String loginName, String password) {

        Connection con = null;
        try {
            con = new Connection(ip, port);
            con.connect();
            boolean isAuthed = isAuth(con, loginName, password);
            if (isAuthed) {
                System.out.println("认证成功!");
                SCPClient scpClient = con.createSCPClient();
                scpClient.get(remoteFile, path);
            } else {
                System.out.println("认证失败!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            con.close();
        }
    }

    public static void getFile(String remoteFile, String path) {
        try {
            connection.connect();
            boolean isAuthed = isAuth();
            if (isAuthed) {
                System.out.println("认证成功!");
                SCPClient scpClient = connection.createSCPClient();
                scpClient.get(remoteFile, path);
            } else {
                System.out.println("认证失败!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            connection.close();
        }
    }

    public static void putFile(String localFile, String remoteTargetDirectory) {
        try {
            connection.connect();
            boolean isAuthed = isAuth();
            if (isAuthed) {
                SCPClient scpClient = connection.createSCPClient();
                scpClient.put(localFile, remoteTargetDirectory);
            } else {
                System.out.println("认证失败!");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            connection.close();
        }
    }

    public static void main(String[] args) {
//        try {
//            getFile("/root/cwrpoint/deploy/20200121/snap_test.jpg", "c://test");
//
//            File file = FileUtil.file("c://test//snap_test.jpg");
//            FileUtil.rename(file,"bbb",true,true);
////            putFile("c://aaa.txt", "/home/users/ubuntu");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


        FileUtil.del("C:\\test\\snap_2020011411001.jpg");
    }

}
